package com.starry.home.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.starry.home.R
import com.starry.home.databinding.ItemSectionParentBinding
import com.starry.home.logic.model.hospital.SectionResponseBean

/**
 * Package:com.starry.dotpen_patients.ui.adapter
 * Author:starr
 * Time:2024/1/6 19:32
 * Description:  科室信息
 */
class SectionParentAdapter(
    private val hospitalSection: List<SectionResponseBean.Page.SectionBean>,

    ) : RecyclerView.Adapter<SectionParentAdapter.SectionParentViewHolder>() {

    private lateinit var itemClickListener: OnItemClickListener

    inner class SectionParentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SectionParentViewHolder {
        val binding = DataBindingUtil.inflate<ItemSectionParentBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_section_parent, parent, false
        )
        return SectionParentViewHolder(binding.root)
    }

    override fun getItemCount(): Int {
        return hospitalSection.size
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun onBindViewHolder(holder: SectionParentViewHolder, position: Int) {
        val binding: ItemSectionParentBinding? = DataBindingUtil.getBinding(holder.itemView)
        binding?.let {
            binding.cbLeftSection.text = hospitalSection[position].emrKsName
            binding.cbLeftSection.isChecked = hospitalSection[position].emrKsCheck
            binding.viewLeftLine.visibility =
                if (hospitalSection[position].emrKsCheck) View.VISIBLE else View.INVISIBLE
            binding.cbLeftSection.setOnClickListener {
                hospitalSection.forEachIndexed { index, item ->
                    item.emrKsCheck = index == position
                }
                notifyDataSetChanged()
                itemClickListener.onItemClick(position)
            }
        }

    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setItemClick(clickItem: OnItemClickListener) {
        this.itemClickListener = clickItem
    }
}